﻿using System.Collections.Generic;

namespace DeviceController.Wpf.Common
{
    public class ViewContext
    {
        public ViewContext()
        {
            _dataContextValues = new Dictionary<string, object>();
        }

        private readonly Dictionary<string, object> _dataContextValues;

        public bool TryGetValue<TValue>(string key, out TValue value)
        {
            var result = _dataContextValues.TryGetValue(key, out var outValue);

            if (result && outValue is TValue newValue)
            {
                value = newValue;
                return true;
            }

            value = default!;
            return false;
        }

        public bool Add<TValue>(string key, TValue value)
        {
            if (value is null)
            {
                return false;
            }

            _dataContextValues[key] = value;

            return true;
        }

        public bool TryRemove(string key)
        {
            if (_dataContextValues.ContainsKey(key))
            {
                _dataContextValues.Remove(key);
                return true;
            }

            return false;
        }
    }
}
